<?php

namespace app\langjinmobile\controller;
//导入基类
use app\common\model\IntegralLog;
use app\common\model\Margin;
use app\common\model\User;
use app\common\model\UserLevel;
use app\langjinmobile\model\Money;
use think\Controller;
use think\facade\Cache;
use think\Db;
use app\common\model\User as userModel;
use app\common\model\Notice;
use app\common\model\UserLevel as userLevelModel;
use app\common\model\Money as moneyModel;
use app\common\model\Reward;
use think\facade\Env;
use app\common\model\Gongpai;
use app\common\model\Business;
class My extends Controller 
{
	public function my(){
		$uid = session('userid');
		$userinfo = userModel::where('id',$uid)->field('id,mobile,is_business,head_img')->find();
		$user_level = userLevelModel::where('userid',$uid)->find();
        $user_level = $this->user_grade($user_level['user_grade']);
		$money = moneyModel::where('userid',$uid)->find();
        $bis_status = Business::where(['userid'=>$uid,'status'=>1])->count();
        $xingji_shouyi = IntegralLog::where('userid',$uid)->where('type','in','1,2,12')->sum('num');
        $juxing_shouyi = IntegralLog::where('userid',$uid)->where('type',15)->sum('num');
		$this->assign('bis_status',$bis_status);
		$this->assign('money',$money);
		$this->assign('user_level',$user_level);
		$this->assign('userinfo',$userinfo);
		$this->assign('xingji_shouyi',$xingji_shouyi);
		$this->assign('juxing_shouyi',$juxing_shouyi);
		return $this->fetch();
	}

    public function user_grade($value){
        $status = [
            0=>'0星会员',1=>'1星会员',2=>'2星会员',3=>'3星会员', 4=>'4星会员',5=>'5星会员',6=>'6星会员', 7=>'7星会员',8=>'8星会员',9=>'9星会员'
        ];
        return $status[$value];
    }

	    // 消息
		public function usermessage(){
            $notices = Notice::where('type' , 1)->order('id desc')->select();
            $this->assign('notices', $notices);
			return $this->fetch(); 
		} 
		// 分红记录
		public function sharerecord(){
			return $this->fetch(); 
        } 
        // 消息详情页
		public function messagedetails($id=''){
            $notice = Notice::where('id',$id)->find();
            $this->assign('notice',$notice);
			return $this->fetch(); 
        }        		 

	public function imgUps()
    {
        if (request()->isPost()) {
            $uid = session('userid');
            $dataflow = trim(input('post.dataflow'));
            $base64 = str_replace('data:image/jpeg;base64,', '', $dataflow);
            $img = base64_decode($base64);
            //生成一个当天年月日文件夹
            $rdate = date('Ymd');
            $mkdir = Env::get('root_path') .'public/uploads/'.$rdate;//文件夹目录
            if(!file_exists($mkdir)){
                mkdir(Env::get('root_path') .'public/uploads/'.$rdate);//创建目录
            }
            //保存地址
            $imgDir = $mkdir;
            //要生成的图片名字
            $filename = '/'.time() . mt_rand(10, 99) . ".png"; //新图片名称
            $newFilePath = $imgDir . $filename;
            $res = file_put_contents($newFilePath, $img);//返回的是字节数
            if ($res > 1000) {
                //删除之前的图片
                $user = userModel::where('id',$uid)->field('head_img')->find();
                if($user['head_img'] != '' && $user['head_img'] != '/static/mobile/images/userhead_img.png'){
                    //file文件路径
                    $unfilename = '.'.$user['head_img'];// dump($filename);//"./uploads/20180710/123.png"
                    if(file_exists($unfilename)){
                        unlink($unfilename);
                    }
                } 
                $data['head_img'] = '/uploads/' . $rdate . $filename;
                $res_change = userModel::where('id',$uid)->update($data);
                if ($res_change) {
                    // ajaxReturn('头像修改成功', 1);
                    return ['code'=>1,'mes'=>'头像修改成功'];

                } else {
                    // ajaxReturn('头像修改失败', 0);
                    return ['code'=>1,'mes'=>'头像修改失败'];

                }
            } else {
                // ajaxReturn('头像修改失败', 0);
                return ['code'=>1,'mes'=>'头像修改失败'];

            }
        }
    }
    public function unactivated(){
        $userid =session('userid');
        //当前会员等级
        $level_grade =  userLevelModel::where('userid',$userid)->value('level_grade');
        $status = [
            '0'=>'普通',
            '1'=>'节点',
            '2'=>'超级节点',
            '3'=>'社区',
        ];
        //升级会员价格
        $info = Reward::where('group',3)->order('id asc')->select();
        $this->assign('level_grade', $status[$level_grade]);
        $this->assign('vip', $info);
        return $this->fetch();
    }
    public function ranking(){
        $userid = session('userid');
        $list = Gongpai::where('userid',$userid)->field('id,qu_id')->select();
        $count = Gongpai::where('userid',$userid)->count();
        $num = IntegralLog::where(['userid'=>$userid,'type'=>15])->sum('num');
        $this->assign('list', $list);
        $this->assign('count', $count);
        $this->assign('num', $num);
        return $this->fetch();
    }
    // 星级图谱
    public function starmap(){
        $userid= session('userid');
        $sql = 'select id from langjin_user where FIND_IN_SET('.$userid.',path)';
        $child_list = Db::query($sql);
        $count = 0;
        $yixing = array('name'=>'一星会员','count'=>0,'num'=>0);
        $erxing = array('name'=>'二星会员','count'=>0,'num'=>0);
        $sanxing = array('name'=>'三星会员','count'=>0,'num'=>0);
        $sixing = array('name'=>'四星会员','count'=>0,'num'=>0);
        $wuxing = array('name'=>'五星会员','count'=>0,'num'=>0);
        $liuxing = array('name'=>'六星会员','count'=>0,'num'=>0);
        $qixing = array('name'=>'七星会员','count'=>0,'num'=>0);
        $baxing = array('name'=>'八星会员','count'=>0,'num'=>0);
        $jiuxing = array('name'=>'九星会员','count'=>0,'num'=>0);
        foreach($child_list as $key=>$value){
            $user_grade = UserLevel::where('userid',$value['id'])->value('user_grade');
            $child_list[$key]['user_grade'] = $user_grade;
           /* if($user_grade==0){
                unset($child_list[$key]);
            }*/
            if($child_list[$key]['user_grade'] == 1){
                $yixing['count'] +=1;
                $zhitui = IntegralLog::where(['userid'=>$userid,'from_userid'=>$value['id']])->where('type','in','1,2')->sum('num');
                $yixing['num'] +=$zhitui;
            }elseif($child_list[$key]['user_grade'] == 2){
                $erxing['count'] +=1;
                $zhitui = IntegralLog::where(['userid'=>$userid,'from_userid'=>$value['id']])->where('type','in','1,2')->sum('num');
                $erxing['num'] +=$zhitui;
            }elseif($child_list[$key]['user_grade'] == 3){
                $sanxing['count'] +=1;
                $zhitui = IntegralLog::where(['userid'=>$userid,'from_userid'=>$value['id']])->where('type','in','1,2')->sum('num');
                $sanxing['num'] +=$zhitui;
            }elseif($child_list[$key]['user_grade'] == 4){
                $sixing['count'] +=1;
                $zhitui = IntegralLog::where(['userid'=>$userid,'from_userid'=>$value['id']])->where('type','in','1,2')->sum('num');
                $sixing['num'] +=$zhitui;
            }elseif($child_list[$key]['user_grade'] == 5){
                $wuxing['count'] +=1;
                $zhitui = IntegralLog::where(['userid'=>$userid,'from_userid'=>$value['id']])->where('type','in','1,2')->sum('num');
                $wuxing['num'] +=$zhitui;
            }elseif($child_list[$key]['user_grade'] == 6){
                $liuxing['count'] +=1;
                $zhitui = IntegralLog::where(['userid'=>$userid,'from_userid'=>$value['id']])->where('type','in','1,2')->sum('num');
                $liuxing['num'] +=$zhitui;
            }elseif($child_list[$key]['user_grade'] == 7){
                $qixing['count'] +=1;
                $zhitui = IntegralLog::where(['userid'=>$userid,'from_userid'=>$value['id']])->where('type','in','1,2')->sum('num');
                $qixing['num'] +=$zhitui;
            }elseif($child_list[$key]['user_grade'] == 8){
                $baxing['count'] +=1;
                $zhitui = IntegralLog::where(['userid'=>$userid,'from_userid'=>$value['id']])->where('type','in','1,2')->sum('num');
                $baxing['num'] +=$zhitui;
            }elseif($child_list[$key]['user_grade'] == 9){
                $jiuxing['count'] +=1;
                $zhitui = IntegralLog::where(['userid'=>$userid,'from_userid'=>$value['id']])->where('type','in','1,2')->sum('num');
                $jiuxing['num'] +=$zhitui;
            }
        }
            $arr = [
                '1'=>$yixing,
                '2'=>$erxing,
                '3'=>$sanxing,
                '4'=>$sixing,
                '5'=>$wuxing,
                '6'=>$liuxing,
                '7'=>$qixing,
                '8'=>$baxing,
                '9'=>$jiuxing,
            ];
        $this->assign('arr', $arr);
        return $this->fetch();
    }
    // 矩阵区域
    public function marea(){
        $userid= session('userid');
        $yixing = array('name'=>'一星区域','zong'=>0,'dongjie'=>0);
        $erxing = array('name'=>'二星区域','zong'=>0,'dongjie'=>0);
        $sanxing = array('name'=>'三星区域','zong'=>0,'dongjie'=>0);
        $sixing = array('name'=>'四星区域','zong'=>0,'dongjie'=>0);
        $wuxing = array('name'=>'五星区域','zong'=>0,'dongjie'=>0);
        $liuxing = array('name'=>'六星区域','zong'=>0,'dongjie'=>0);
        $qixing = array('name'=>'七星区域','zong'=>0,'dongjie'=>0);
        $baxing = array('name'=>'八星区域','zong'=>0,'dongjie'=>0);
        $jiuxing = array('name'=>'九星区域','zong'=>0,'dongjie'=>0);
        //一星
        $yi_zong = IntegralLog::where(['type'=>15,'status'=>0,'qu_id'=>1,'userid'=>$userid])->sum('num');
        $yi_dong =IntegralLog::where(['type'=>15,'status'=>1,'qu_id'=>1,'userid'=>$userid])->sum('num');
        $yixing['zong'] = $yi_zong;
        $yixing['dongjie'] = $yi_dong;
        //二星
        $er_zong = IntegralLog::where(['type'=>15,'status'=>0,'qu_id'=>2,'userid'=>$userid])->sum('num');
        $er_dong =IntegralLog::where(['type'=>15,'status'=>1,'qu_id'=>2,'userid'=>$userid])->sum('num');
        $erxing['zong'] = $er_zong;
        $erxing['dongjie'] = $er_dong;
        //三星
        $san_zong = IntegralLog::where(['type'=>15,'status'=>0,'qu_id'=>3,'userid'=>$userid])->sum('num');
        $san_dong =IntegralLog::where(['type'=>15,'status'=>1,'qu_id'=>3,'userid'=>$userid])->sum('num');
        $sanxing['zong'] = $san_zong;
        $sanxing['dongjie'] = $san_dong;
        //四星
        $si_zong = IntegralLog::where(['type'=>15,'status'=>0,'qu_id'=>4,'userid'=>$userid])->sum('num');
        $si_dong =IntegralLog::where(['type'=>15,'status'=>1,'qu_id'=>4,'userid'=>$userid])->sum('num');
        $sixing['zong'] = $si_zong;
        $sixing['dongjie'] = $si_dong;
        //五星
        $wu_zong = IntegralLog::where(['type'=>15,'status'=>0,'qu_id'=>5,'userid'=>$userid])->sum('num');
        $wu_dong =IntegralLog::where(['type'=>15,'status'=>1,'qu_id'=>5,'userid'=>$userid])->sum('num');
        $wuxing['zong'] = $wu_zong;
        $wuxing['dongjie'] = $wu_dong;
        //六星
        $liu_zong = IntegralLog::where(['type'=>15,'status'=>0,'qu_id'=>6,'userid'=>$userid])->sum('num');
        $liu_dong =IntegralLog::where(['type'=>15,'status'=>1,'qu_id'=>6,'userid'=>$userid])->sum('num');
        $liuxing['zong'] = $liu_zong;
        $liuxing['dongjie'] = $liu_dong;
        //七星
        $qi_zong = IntegralLog::where(['type'=>15,'status'=>0,'qu_id'=>7,'userid'=>$userid])->sum('num');
        $qi_dong =IntegralLog::where(['type'=>15,'status'=>1,'qu_id'=>7,'userid'=>$userid])->sum('num');
        $qixing['zong'] = $qi_zong;
        $qixing['dongjie'] = $qi_dong;
        //八星
        $ba_zong = IntegralLog::where(['type'=>15,'status'=>0,'qu_id'=>8,'userid'=>$userid])->sum('num');
        $ba_dong =IntegralLog::where(['type'=>15,'status'=>1,'qu_id'=>8,'userid'=>$userid])->sum('num');
        $baxing['zong'] = $ba_zong;
        $baxing['dongjie'] = $ba_dong;
        //五星
        $jiu_zong = IntegralLog::where(['type'=>15,'status'=>0,'qu_id'=>9,'userid'=>$userid])->sum('num');
        $jiu_dong =IntegralLog::where(['type'=>15,'status'=>1,'qu_id'=>9,'userid'=>$userid])->sum('num');
        $jiuxing['zong'] = $jiu_zong;
        $jiuxing['dongjie'] = $jiu_dong;
        $arr = [
            '1'=>$yixing,
            '2'=>$erxing,
            '3'=>$sanxing,
            '4'=>$sixing,
            '5'=>$wuxing,
            '6'=>$liuxing,
            '7'=>$qixing,
            '8'=>$baxing,
            '9'=>$jiuxing,
        ];
        //获取当前会员等级
        $user_grade = UserLevel::where('userid',session('userid'))->value('user_grade');
        $this->assign('user_grade',$user_grade);
        $this->assign('arr', $arr);
        return $this->fetch();
    }
}